﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace NullCommander
{
    static class ClassHelpers
    {
        public const String DATE_TIME_FORMAT = "dd MMMM yyyy HH:mm:ss";
        
        /// <summary>
        /// Хелпер, позволяет искать ноду в дереве по тексту.
        /// </summary>
        /// <param name="nodes"></param>
        /// <param name="text"></param>
        /// <returns>Найденный нод или null</returns>
        static public TreeNode NodeByText(this TreeNodeCollection nodes, String text)
        {
            foreach (TreeNode node in nodes)
                if (String.Compare(node.Text, text, true, CultureInfo.CurrentCulture) == 0)
                    return node;
            return null;
        }

        /// <summary>
        /// Метод добавляет разделитель пути, если он отсутствует.
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        static public String IncludePathDelimiter(this String path)
        {
            if (path == null)
                return null;
            var i = path.Length;
            if (i == 0)
                return String.Empty;
            return path[--i] != Path.DirectorySeparatorChar ? path + Path.DirectorySeparatorChar : path;
        }

        /// <summary>
        /// Метод убирает разделитель пути, если он есть.
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        static public String ExcludePathDelimiter(this String path)
        {
            if (path == null)
                return null;
            var i = path.Length;
            if (i == 0)
                return String.Empty;
            return path[--i] != Path.DirectorySeparatorChar ? path : path.Remove(i);
        }

        static public Boolean IsInit(this TreeNode node)
        {
            return node.Tag != null;
        }

        // From: http://stackoverflow.com/questions/281640/how-do-i-get-a-human-readable-file-size-in-bytes-abbreviation-using-net
        static public String FileSizeToString(Int64 size)
        {
            string[] sizes = { "B", "KB", "MB", "GB" };
            double len = size;
            int order = 0;
            while (len >= 1024 && order + 1 < sizes.Length)
            {
                order++;
                len = len / 1024;
            }

            // Adjust the format string to your preferences. For example "{0:0.#}{1}" would
            // show a single decimal place, and no space.
            return String.Format("{0:0.##} {1}", len, sizes[order]);
        }


    }
}
